package com.glela.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.activ.model.OActivContentExpress;
import com.glela.activ.model.OActivContentMoney;
import com.glela.activ.model.OActivGiftProduct;
import com.glela.activ.model.OActivitiesInfo;
import com.glela.activ.service.OActivService;
import com.glela.goods.model.request.ActivitiesRequest;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;

/**
* @ClassName: ActivitiesController 
* @Description: 小铺来了商品明细活动类控制器
* @author NingChongQing
* @date 2017年10月16日 上午11:37:22
 */
@Controller
@RequestMapping(value = "/activities")
public class ActivitiesController {
	private static Logger logger = LoggerFactory.getLogger(ActivitiesController.class);
	
	@Autowired
	private OActivService oActivService;
	/**
	 * 
	* **********************************************
	* @Title: popover 
	* @Description: 活动信息弹窗接口
	* @param @param request
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author NingChongQing	
	* @date 2017年10月20日 下午4:13:11 
	* **********************************************
	 */
	@RequestMapping(value = "/popover", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> popover(@RequestBody ActivitiesRequest request) {
		try {
			if (ParameterUtils.validParams(request.getGoodsId()) && ParameterUtils.validParams(request.getActivitiesId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			String activName = "";
			List<Map<String, Object>> activitiesList = new ArrayList<Map<String,Object>>();
			OActivitiesInfo oActivitiesInfo = this.oActivService.getOActivitiesInfo(request.getGoodsId(),request.getActivitiesId());
			if (oActivitiesInfo != null) {
				activName = oActivitiesInfo.getoActiv().getActivName();
				if (!CollectionUtils.isEmpty(oActivitiesInfo.getoActivContentExpressList())){
					Map<String, Object> oagfMap = new HashMap<String, Object>();
					String tag = "";
					List<Map<String, Object>> oaList = new ArrayList<Map<String,Object>>();
					for (OActivContentExpress oace : oActivitiesInfo.getoActivContentExpressList()) {
						Map<String, Object> oaMap = new HashMap<String, Object>();
						oaMap.put("slogan", oace.getActivTitle());
						oaMap.put("number", 1);
						oaMap.put("jumpParameter", 0);
						oaList.add(oaMap);
						if ("".equals(tag)) {
							tag = oace.getTagName();
						}
					}
					oagfMap.put("tag",tag);
					oagfMap.put("eventType",1);
					oagfMap.put("list",oaList);
					activitiesList.add(oagfMap);
				}
				if (!CollectionUtils.isEmpty(oActivitiesInfo.getoActivContentMoneyList())){
					Map<String, Object> oagfMap = new HashMap<String, Object>();
					String oacmTag = "";
					String oacmSlogan = "";
					for (OActivContentMoney oacm : oActivitiesInfo.getoActivContentMoneyList()) {
						if (StringUtils.isEmpty(oacmTag)) {
							oacmTag = oacm.getTagName();
						}
						oacmSlogan = oacmSlogan + oacm.getActivTitle() + ";";
					}
					List<Map<String, Object>> oaList = new ArrayList<Map<String,Object>>();
					Map<String, Object> oaMap = new HashMap<String, Object>();
					oaMap.put("slogan", oacmSlogan);
					oaMap.put("number", 1);
					oaMap.put("jumpParameter", oActivitiesInfo.getoActiv().getId());
					oaList.add(oaMap);
					oagfMap.put("tag",oacmTag);
					oagfMap.put("eventType",0);
					oagfMap.put("list",oaList);
					activitiesList.add(oagfMap);
				}
				if (!CollectionUtils.isEmpty(oActivitiesInfo.getoActivGiftProductList())){
					Map<String, Object> oagfMap = new HashMap<String, Object>();
					String tag = "";
					List<Map<String, Object>> oaList = new ArrayList<Map<String,Object>>();
					for (OActivGiftProduct oagf : oActivitiesInfo.getoActivGiftProductList()) {
						Map<String, Object> oaMap = new HashMap<String, Object>();
						oaMap.put("slogan", oagf.getActivTitle());
						oaMap.put("number", oagf.getGiftNum());
						oaMap.put("jumpParameter", oagf.getGoodsId());
						oaList.add(oaMap);
						if ("".equals(tag)) {
							tag = oagf.getTagName();
						}
					}
					oagfMap.put("tag",tag);
					oagfMap.put("eventType",2);
					oagfMap.put("list",oaList);
					activitiesList.add(oagfMap);
				}
			}
			Map<String, Object> activities = new HashMap<String, Object>();
			activities.put("activName", activName);
			activities.put("activitiesList", activitiesList);
			return new ResponseEntity<>(new ResponseResultModel<>(activities), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取<活动信息>异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
}
